app.controller('fourteen', function ($scope, $rootScope, $http, $interval, $compile, $state) {
    //详情页
    $scope.az2 = function (caseId) {
        $state.go(az2.stateName);
    }
    //获得数据
    $scope.submit = function () {
        if(!$scope.caseId){
            return;
        }

        if ($("#signature").jSignature("getData", "native").length == 0) {
            $rootScope.tipPopups.show(($rootScope.tipMessage = "请先签字"));
            return false;
        }

        importSign('signature', 'signature', function (result) {
           var json = JSON.stringify(result)
           var jsonData = JSON.parse(json).obj;
           for (var i = 0; i < jsonData.length; i++) {
               url = jsonData[i].url;
           }
           console.log("result---",url);
            if(!url){
                $rootScope.tipPopups.show(($rootScope.tipMessage = "请先签字"));
                return;
            }

            if(2==$scope.signType){
                $scope.feSignUrl = url;
                //存签名
                localStorage.setItem("sdm-feSignUrl",url);
            }else if(1==$scope.signType){
                $scope.customerSignUrl = url;
                //存签名
                localStorage.setItem("sdm-customerSignUrl",url);
            }

            $scope.customerEmail = $(".customer-email").val();
            ///提交签名
            var postData = {
                "ids": [$scope.caseId],
                "feSignUrl": $scope.feSignUrl,
                "customerSignUrl": $scope.customerSignUrl,
                "customerEmail":$scope.customerEmail
            };
            postHttp($http, 'installtionCase/commitSign', postData, function (res) {
                if (res.code === 0 && res.msg == "success") {
                    $scope.az2();
                    $scope.$applyAsync();
                } else {
                    $rootScope.tipPopups.show(($rootScope.tipMessage = res.msg));
                }
            });

        });

    }

    //初始化
    function init(){
        $scope.feSignUrl = "";
        $scope.customerSignUrl = "";

        //签名类型
        $scope.signType = sixteen.params.type;
        if(!sixteen.params.type){
            $scope.signType = localStorage.getItem("sdm-type");
        }
        $scope.signTypeName = "";
        if(1==$scope.signType){
            $scope.signTypeName = "客户";
        }else{
            $scope.signTypeName = "工程师";
        }

        //案例id
        $scope.caseId = sixteen.params.caseId;
        if(!sixteen.params.caseId){
            $scope.caseId = localStorage.getItem("sdm-caseId");
        }
    }

    init();


    //初始化画布
    $(document).ready(function(){
        var innnerheight = window.innerHeight;
        var deHeight = 200;
        if(1!=$scope.signType){
            deHeight = 250;
        }

        var height=(innnerheight - deHeight )+'px';
        document.getElementById("sign").style.height = height;
        var width=100+'%';
        $("#signature").jSignature({height:height,width:width,color:"#000",lineWidth:4});
    })
    //重置画布
    $scope.clearSig = function(){
        $("#signature").jSignature("clear");
    }
    //将画布内容显示到IMG
    $scope.importImg = function(){
        var url = "";
        importSign('signature', 'signature', function (result) {
			var json = JSON.stringify(result)
			var jsonData = JSON.parse(json).obj;
			for (var i = 0; i < jsonData.length; i++) {
			    url = jsonData[i].url;
			}
            if(1==$scope.signType){
                $scope.feSignUrl = url;
                //存签名
                localStorage.setItem("sdm-feSignUrl",url);
            }else if(2==$scope.signType){
                $scope.customerSignUrl = url;
                //存签名
                localStorage.setItem("sdm-customerSignUrl",url);
            }
        });
        return url;
    }

    reloadJs();

});